/**
 * 
 */
package com.miolr.core.struts2;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import org.apache.struts2.rest.handler.JacksonLibHandler;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectReader;
import org.codehaus.jackson.map.SerializationConfig.Feature;

import com.miolr.core.jackson.HibernateSerializerFactory;

/**
 * @author iday
 *
 */
public class JacksonHibernateLibHandler extends JacksonLibHandler {
    private ObjectMapper mapper = new ObjectMapper();

	/* (non-Javadoc)
	 * @see org.apache.struts2.rest.handler.JacksonLibHandler#fromObject(java.lang.Object, java.lang.String, java.io.Writer)
	 */
	@Override
	public String fromObject(Object obj, String resultCode, Writer stream)
			throws IOException {
		mapper.configure(Feature.WRITE_NULL_MAP_VALUES, false);
		mapper.setSerializerFactory(HibernateSerializerFactory.instance);
        mapper.writeValue(stream, obj);
        return null;
	}

	/* (non-Javadoc)
	 * @see org.apache.struts2.rest.handler.JacksonLibHandler#toObject(java.io.Reader, java.lang.Object)
	 */
	@Override
	public void toObject(Reader in, Object target) throws IOException {
        mapper.configure(Feature.WRITE_NULL_MAP_VALUES, false);
        ObjectReader or = mapper.readerForUpdating(target);
        or.readValue(in); //, new TypeReference<clazz>);
	}

}
